package controller;

import conventer.ResultInfo;
import dao.GoodDAO;
import dao.UserDAO;
import entity.GoodEntity;
import entity.UserEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import utilities.LoginTestUtil;
import utilities.MailUtil;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static org.springframework.web.bind.annotation.RequestMethod.GET;


@Controller
public class LoginController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(){
        return "login.html";
    }

    @RequestMapping(value = {"/index","/index.html"}, method = GET)
    public String getIndex(HttpSession session){
        if(LoginTestUtil.isLoggedin(session))
            return "index.html";
        else
            return "login.html";
    }

    @RequestMapping(value = "/load", method = GET)
    @ResponseBody
    public Object getIndex2(HttpSession session){
        Map<String,Object> result = new HashMap<>();
        if(LoginTestUtil.isLoggedin(session))
            result.put("result","yes");
        else
            result.put("result","no");
        //不能在返回语句中写put方法，因为这样的话返回的不是对象而是put方法的返回值
        return result;
    }


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Object login(@RequestParam String email,
                        @RequestParam String password,
                        HttpSession session){
        ResultInfo result = new ResultInfo();
        if(UserDAO.login(email,password)){
            session.setAttribute("userid",UserDAO.getIDByEmail(email));
            session.setMaxInactiveInterval(300);
            result.setResult("SUCCESS");
        }else{
            result.setResult("FAILED");
            result.setReason("WRONG EMAIL OR PASSWORD");
        }

        return result;
    }

    @RequestMapping(value = "/forget", method = RequestMethod.POST)
    @ResponseBody
    public Object sendPassword(@RequestParam(value = "email", required = true) String email){
        ResultInfo result = new ResultInfo();
        String password = UserDAO.getPasswordByEmail(email);
        if(password == null || password.equals("")){
            result.setResult("WRONG");
            result.setReason("THIS EMAIL IS NOT REGISTED");
            return result;
        }
        if(MailUtil.sendPassword(email,password))
            result.setResult("SUCCESS");
        else{
            result.setResult("FAILED");
            result.setReason("SEND PASSWORD FAILED");
        }
        return result;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public Object logout(HttpSession session){
        ResultInfo result = new ResultInfo();
        if(session.getAttribute("userid") != null && Integer.parseInt(session.getAttribute("userid").toString())>=0){
            session.setAttribute("userid",-1);
            result.setResult("SUCCESS");
            LoginTestUtil.isLoggedin(session);
        }else{
            result.setResult("ERROR");
            result.setReason("YOU ARE NOT LOGGED IN");
        }

        return result;
    }

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public Object getUserByID(@RequestParam int id,
                                  HttpSession session){
        ResultInfo result = new ResultInfo();
        if(!LoginTestUtil.isLoggedin(session)) {
            result.setResult("ERROR");
            result.setReason("YOU ARE NOT LOGGED IN");
            return result;
        }


        UserEntity user = UserDAO.getUserByID(id);
        if(user == null){
            result.setResult("FAILED");
            result.setReason("GET USERNAME FAILED");
            result.setData(id);
        }else{
            result.setResult("SUCCESS");
            result.setData(user);
        }
        return result;
    }
}
